package com.eventosgasteiz.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.widget.Toast;

public class ImageHandler {
	public static final String extStorageDirectory = "/sdcard/";
	private Context context;
	public ImageHandler(Context context){
		this.context=context;
	}
	public void saveImage(Bitmap image,String filename){
		OutputStream outStream = null;
		
		   File file = new File(extStorageDirectory+ filename);
		   try {
		    outStream = new FileOutputStream(file);
		    image.compress(Bitmap.CompressFormat.PNG, 100, outStream);
		    outStream.flush();
		    outStream.close();		   		    
		   
		   } catch (FileNotFoundException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		    Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
		   } catch (IOException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		    Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
		   }
	}
		
		
	public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
			 
			int width = bm.getWidth();
			 
			int height = bm.getHeight();
			 
			float scaleWidth = ((float) newWidth) / width;
			 
			float scaleHeight = ((float) newHeight) / height;
			 
			// create a matrix for the manipulation
			 
			Matrix matrix = new Matrix();
			 
			// resize the bit map
			 
			matrix.postScale(scaleWidth, scaleHeight);
			 
			// recreate the new Bitmap
			 
			Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
			 
			return resizedBitmap;
			 
		}

}
